<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
    <BasicForm @register="registerForm"/>
  </BasicModal>
</template>
<script lang="ts" setup>
import {ref, computed, unref} from 'vue';
import {BasicModal, useModalInner} from '@/components/Modal';
import {BasicForm, useForm} from '@/components/Form';
import {resetPwdFromSchema} from './user.data';
import {resetPwd} from "@/api/system/user";

defineOptions({name: 'AccountModal'});

const emit = defineEmits(['success', 'register']);

const userId = ref('');


const [registerForm, {setFieldsValue, updateSchema, resetFields, validate}] = useForm({
  labelWidth: 100,
  baseColProps: {span: 24},
  schemas: resetPwdFromSchema,
  showActionButtonGroup: false,
  actionColOptions: {
    span: 23,
  },
});

const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
  resetFields();
  setModalProps({confirmLoading: false});
  userId.value = data.record.userId;
  // setFieldsValue({
  //   ...data.record,
  // });


});

const getTitle = computed(() => ("重置密码"));

async function handleSubmit() {
  try {
    const values = await validate();
    setModalProps({confirmLoading: true});
    const res = await resetPwd({...values, userId: userId.value})
    closeModal();
    emit('success');
  } catch (e) {
    console.log(e, "捕获异常")
  } finally {
    setModalProps({confirmLoading: false});
  }
}
</script>
